package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.syslog.tool.SysTraceLogWriter;
import com.augurit.gzcgwsp.lhsp.service.ILhspUpdateFlowStateService;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.printDoc.web.form.YwPrintDocForm;
import com.augurit.gzcgwsp.sendNotice.dao.YwSphjInfoDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwSphjInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwSphjInfoService;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceForWbService;
import com.augurit.gzcgwsp.sendNotice.service.IYwZhengwuInfoService;
import com.augurit.gzcgwsp.webservice.service.IKingdomDataForRqsjService;
import com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfTemplateAttachmentRefServiceEX;
import com.augurit.gzcgwsp.wfex.common.util.WfUtilsEX;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;

/**
 * 审批环节--实体
 * 
 *
 */
@Service
@Transactional
public class YwSphjInfoServiceImpl implements IYwSphjInfoService{
	Logger logger = (Logger) LoggerFactory.getLogger(YwSphjInfoServiceImpl.class);
	@Autowired
	private YwSphjInfoDao ywSphjInfoDao;	
	@Autowired
	private IYwZhengwuInfoService ywZhengwuInfoService;	
    @Autowired
	private IYwComSjcllistService ywComSjcllistService;    
	@Autowired
	private IYwComSjclService ywComSjclService;	
    @Autowired
	private IYwPrintDocService ywPrintDocService;	
    @Autowired
    private IYwComQyxxService ywComQyxxService;
    @Autowired
    private IOaDocCodeServiceEX oaDocCodeServiceEX;
    @Autowired
    private IWfTemplateAttachmentRefServiceEX wfTemplateAttachmentRefServiceEX;
    @Autowired
    private IYwWebserviceForWbService ywWebserviceForWbService;
	@Autowired
	private IKingdomDataForRqsjService kingdomDataForRqsjService;
	@Autowired
	private ILhspUpdateFlowStateService lhspUpdateFlowStateService;

	/**
	 * 保存 ywMtbjlshInfo
	 * @param ywMtbjlshInfo 
	 */
	public void saveOrUpdateYwSphjInfo(YwSphjInfo ywSphjInfo) {
		ywSphjInfoDao.getSession().saveOrUpdate(ywSphjInfo);
	}
	
	/**
	 * 获取 ywMtbjlshInfo
	 * @param id 
	 */
	public YwSphjInfo getYwSphjInfo(Long id) {
		return ywSphjInfoDao.get(id);
	}
	
	/**
	 * 获取所有 流水号
	 * @param date 
	 */
	public List<YwSphjInfo> getAll() {		
		return ywSphjInfoDao.getAll();
	}

    public void delete(YwSphjInfo entity) {
    	ywSphjInfoDao.delete(entity);
        
    }
    
	public Page<YwSphjInfo> getByMasterEntityKey(Page<YwSphjInfo> ywSphjInfosPage,String masterEntityKey,String templateCode) {
    	String sql = "from YwSphjInfo s where s.masterEntityKey='" + masterEntityKey + "' and s.templateCode='"+templateCode+"' order by s.id desc";
        List<YwSphjInfo> entityList = ywSphjInfoDao.find(sql);
        //return entityList;
        
        // 查询语句及参数
        StringBuffer hql = new StringBuffer();
        hql.append(sql);
        List values = new ArrayList();        
        // 执行分页查询操作
        ywSphjInfoDao.findPage(ywSphjInfosPage, hql.toString(), values);
        
        return ywSphjInfosPage;
    }
	
	/**
	 * 公共方法 ，窗口收案开始，共5个节点
	 * 【窗口收案发出（发出时间为准）--经办人初审（发出）--处主管领导审核（发出）--委分管领导审核（发出）--经办人办结（发出）】    数据交换
	 * @param flag						标识，标识是哪个业务的
	 * @param jc						配置的简称(根据interfaceExchange.properties的配置取，如ssgd001,就取ssgd)
	 * @param thisNoteActivityName		这个节点的英文名称
	 * @param nextNoteActivityName		下个节点的英文名称
	 * @param masterEntityKey			案件ID
	 * @param instance
	 * @param loginUserName				登录用户名
	 * @param dpm						部门
	 * @param durationExpression
	 * @param localResource
	 * @param ywRqSsgdSqbForm
	 * @param sprzwdm
	 * @param sprzwmc
	 * @param iswb						是否网办
	 */
	public void commonZwBeginByCksa(int flag, Object obj, String jc,String thisNoteActivityName,String nextNoteActivityName,String masterEntityKey,
			WfBusInstanceTaskForm instance,String loginUserName,String dpm, String durationExpression,
			ResourceBundle localResource, String sprzwdm,String sprzwmc,boolean iswb,Map<String,String> map,String sxbm) throws Exception{
		//联合审批工程ID
		String projectId = lhspUpdateFlowStateService.getProjectIdByAcceptNoAndTemplateCode((String)map.get("acceptNo"), instance.getTemplateCode());
		String link001 = localResource.getString(jc + "001");//受理
		String link002 = localResource.getString(jc + "002");//承办
		String link003 = localResource.getString(jc + "003");//审核
		String link004 = localResource.getString(jc + "004");//审批
		String link005 = localResource.getString(jc + "005");//办结
		String link006 = "";//余泥处办事结果
		if(jc.startsWith("jz")){//以jz开头，则是余泥处，余泥处办结和办事结果不一起传，要特殊处理
			link006 = localResource.getString(jc + "006");//余泥处办事结果
		}
		String link = "";//交换给方欣的标识
		List<String> linkList =new ArrayList<String>();//交换给政务中心的标识
		YwSphjInfo ywSphjInfo = this.getYwSphjInfo(masterEntityKey,instance.getTemplateCode());
		ywSphjInfo.setTemplateCode(instance.getTemplateCode());
		if(thisNoteActivityName.equals(link001)){//受理，窗口发出
			link = "001";
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
				if(StringUtils.isNotBlank(projectId)){
					//市的排放、消纳、道路占用、挖掘，需要联合审批的受理环节，保存数据到数据库
					lhspUpdateFlowStateService.saveLhspUpdateFlowState(instance.getTemplateCode(), (String)map.get("acceptNo"), projectId, "CHENGGUANWEI", "受理", instance.getHandleComments(), "同意");
				}
			}
		}
		if(thisNoteActivityName.equals(link002)){//承办，经办人初审发出，要判断前面的节点是否交换了：受理
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
			}
			if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
			if(nextNoteActivityName.equals("dzgz") || nextNoteActivityName.equals("ckfsgz")){//经办人初审发出，发送到窗口发书盖章
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
		}
		if(thisNoteActivityName.equals(link003)){//审核，处主管发出，要判断前面的节点是否交换了：受理、承办
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
			}
			if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
		}
		if(link004.equals(link005)){//余泥处特殊，第4和第5个节点相同，相当于只有4个节点
			if(thisNoteActivityName.equals(link004) || thisNoteActivityName.equals(link005)){//审批、办结，要判断前面的节点是否交换了：受理、承办、审核
				link = "005";
				if(StringUtils.isNotBlank(projectId)){
					//市的排放、消纳、道路占用、挖掘，需要联合审批的办结环节，保存数据到数据库
					String bljg = lhspUpdateFlowStateService.getBljgByAcceptNoAndTemplateCode(masterEntityKey, instance.getTemplateCode());
					if(StringUtils.isNotBlank(bljg)){
						lhspUpdateFlowStateService.saveLhspUpdateFlowState(instance.getTemplateCode(), (String)map.get("acceptNo"), projectId, "CHENGGUANWEI", "办结", instance.getHandleComments(), bljg);
					}
				}
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
//					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
		}else{
			if(thisNoteActivityName.equals(link004)){//审批，委分管发出，要判断前面的节点是否交换了：受理、承办、审核
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
			}
			if(thisNoteActivityName.equals(link005)){//办结，要判断前面的节点是否交换了：受理、承办、审核、审批
				link = "005";
				if(StringUtils.isNotBlank(projectId)){
					//市的排放、消纳、道路占用、挖掘，需要联合审批的办结环节，保存数据到数据库
					String bljg = lhspUpdateFlowStateService.getBljgByAcceptNoAndTemplateCode(masterEntityKey, instance.getTemplateCode());
					if(StringUtils.isNotBlank(bljg)){
						lhspUpdateFlowStateService.saveLhspUpdateFlowState(instance.getTemplateCode(), (String)map.get("acceptNo"), projectId, "CHENGGUANWEI", "办结", instance.getHandleComments(), bljg);
					}
				}
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
					//生成经营许可和企业资质企业、站点信息XML，用于交换给金证
					this.createXML2Jz(instance, masterEntityKey, map);
				}
			}
		}
		if(thisNoteActivityName.equals(link006) && StringUtils.isNotBlank(link006)){//余泥处、窗口发书盖章发出，交换办事结果
			linkList.add("006");
		}
		if(StringUtils.isNotBlank(sxbm)){//交换到政务办
			if(StringUtils.isNotBlank(ywSphjInfo.getMasterEntityKey())){
				this.saveOrUpdateYwSphjInfo(ywSphjInfo);
			}
			this.saveZw(flag, thisNoteActivityName, obj, instance, loginUserName, iswb, durationExpression, sprzwdm, sprzwmc, linkList,map);
		}
		
		//（网办）交换到方欣（市民网页）
		ywWebserviceForWbService.setValueAndSaveYwWebserviceForWb(loginUserName,instance.getTemplateCode(),
					instance.getMasterEntityKey(),durationExpression,link,"", map);		
	}
	
	/**
	 * 公共方法,窗口收案开始，共4个节点（窗口收案--处主管领导审核--委分管领导审核--经办人办结）    数据交换
	 * @param flag						标识，标识是哪个业务的
	 * @param jc						配置的简称(根据interfaceExchange.properties的配置取，如ssgd001,就取ssgd)
	 * @param thisNoteActivityName		这个节点的英文名称
	 * @param nextNoteActivityName		下个节点的英文名称
	 * @param masterEntityKey			案件ID
	 * @param instance
	 * @param loginUserName				登录用户名
	 * @param dpm						部门
	 * @param durationExpression
	 * @param localResource
	 * @param ywRqSsgdSqbForm
	 * @param sprzwdm
	 * @param sprzwmc
	 * @param iswb						是否网办
	 */
	public void commonZwBeginByCksa2(int flag, Object obj, String jc,String thisNoteActivityName,String nextNoteActivityName,String masterEntityKey,
			WfBusInstanceTaskForm instance,String loginUserName,String dpm, String durationExpression,
			ResourceBundle localResource, String sprzwdm,String sprzwmc,boolean iswb,Map<String,String> map){
		
		String link001 = localResource.getString(jc + "001");//受理
//		String link002 = localResource.getString(jc + "002");//承办
		String link003 = localResource.getString(jc + "003");//审核
		String link004 = localResource.getString(jc + "004");//审批
		String link005 = localResource.getString(jc + "005");//办结
		
		List<String> linkList =new ArrayList<String>();
		YwSphjInfo ywSphjInfo = this.getYwSphjInfo(masterEntityKey,instance.getTemplateCode());
		ywSphjInfo.setTemplateCode(instance.getTemplateCode());
		if(thisNoteActivityName.equals(link001)){//窗口收案环节
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				linkList.add("002");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
				ywSphjInfo.setIsChengban("yes");
			}					
		}else if((thisNoteActivityName.equals("jbrsl") || thisNoteActivityName.equals("jbrcs")) && (nextNoteActivityName.equals("dzgz") || nextNoteActivityName.equals("ckfsgz"))){//经办人初审环节 发到 电子盖章
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun() == null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
			if(ywSphjInfo.getIsBanjie() == null || !ywSphjInfo.getIsBanjie().equals("yes")){
				linkList.add("005");
				linkList.add("006");
				ywSphjInfo.setIsBanjie("yes");
			}
		}else if(thisNoteActivityName.equals(link003)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
		}else if(thisNoteActivityName.equals(link004)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
		}else if(thisNoteActivityName.equals(link005)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
			if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
				linkList.add("005");
				linkList.add("006");
				ywSphjInfo.setIsBanjie("yes");
			}
		}
		if(StringUtils.isNotBlank(ywSphjInfo.getMasterEntityKey())){
			this.saveOrUpdateYwSphjInfo(ywSphjInfo);
		}
		this.saveZw(flag, thisNoteActivityName, obj, instance, loginUserName, iswb, durationExpression, sprzwdm, sprzwmc, linkList,map);
	}
	
	/**
	 * 公共方法 ，经办人初审开始，共5个节点（经办人初审--处主管领导审核--委分管领导审核--XXX--经办人办结）    数据交换
	 * @param flag						标识，标识是哪个业务的
	 * @param jc						配置的简称(根据interfaceExchange.properties的配置取，如ssgd001,就取ssgd)
	 * @param thisNoteActivityName		这个节点的英文名称
	 * @param nextNoteActivityName		下个节点的英文名称
	 * @param masterEntityKey			案件ID
	 * @param instance
	 * @param loginUserName				登录用户名
	 * @param dpm						部门
	 * @param durationExpression
	 * @param localResource
	 * @param ywRqSsgdSqbForm
	 * @param sprzwdm
	 * @param sprzwmc
	 * @param iswb						是否网办
	 */
	public void commonZwBeginByJbrsl(int flag, Object obj, String jc,String thisNoteActivityName,String nextNoteActivityName,String masterEntityKey,
			WfBusInstanceTaskForm instance,String loginUserName,String dpm, String durationExpression,
			ResourceBundle localResource, String sprzwdm,String sprzwmc,boolean iswb,Map<String,String> map){
		
		String link001 = localResource.getString(jc + "001");//受理
		String link002 = localResource.getString(jc + "002");//承办
		String link003 = localResource.getString(jc + "003");//审核
		String link004 = localResource.getString(jc + "004");//审批
		String link005 = localResource.getString(jc + "005");//办结
		
		List<String> linkList =new ArrayList<String>();
		YwSphjInfo ywSphjInfo = this.getYwSphjInfo(masterEntityKey,instance.getTemplateCode());
		ywSphjInfo.setTemplateCode(instance.getTemplateCode());
		/*if(nextNoteActivityName.equals(link001)){
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
			}
		}else */
		if(thisNoteActivityName.equals(link001)){//经办人初审环节
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
			}
			if(nextNoteActivityName.equals("dzgz") || nextNoteActivityName.equals("ckfsgz")){//经办人初审环节 发到 电子盖章
				if(ywSphjInfo.getIsChengban() == null || !ywSphjInfo.getIsChengban().equals("yes")){
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun() == null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie() == null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
			if(nextNoteActivityName.contains("end")){//经办人初审环节 直接结案
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun() == null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie() == null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
		}else if(thisNoteActivityName.equals(link002)){
			if(ywSphjInfo.getIsChengban() == null || !ywSphjInfo.getIsChengban().equals("yes")){
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
		}else if(thisNoteActivityName.equals(link003)){
			if(ywSphjInfo.getIsChengban() == null || !ywSphjInfo.getIsChengban().equals("yes")){
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
		}else if(thisNoteActivityName.equals(link004)){
			if(ywSphjInfo.getIsChengban() == null || !ywSphjInfo.getIsChengban().equals("yes")){
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
		}else if(thisNoteActivityName.equals(link005)){
			if(ywSphjInfo.getIsChengban() == null || !ywSphjInfo.getIsChengban().equals("yes")){
				linkList.add("002");
				ywSphjInfo.setIsChengban("yes");
			}
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
			if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
				linkList.add("005");
				linkList.add("006");
				ywSphjInfo.setIsBanjie("yes");
			}
		}
		if(StringUtils.isNotBlank(ywSphjInfo.getMasterEntityKey())){
			this.saveOrUpdateYwSphjInfo(ywSphjInfo);
		}
		this.saveZw(flag, thisNoteActivityName, obj, instance, loginUserName, iswb, durationExpression, sprzwdm, sprzwmc, linkList,map);
	}
	
	/**
	 * 公共方法 ，经办人初审开始，共4个节点（经办人初审--处主管领导审核--委分管领导审核--经办人办结）    数据交换
	 * @param flag						标识，标识是哪个业务的
	 * @param jc						配置的简称(根据interfaceExchange.properties的配置取，如ssgd001,就取ssgd)
	 * @param thisNoteActivityName		这个节点的英文名称
	 * @param nextNoteActivityName		下个节点的英文名称
	 * @param masterEntityKey			案件ID
	 * @param instance
	 * @param loginUserName				登录用户名
	 * @param dpm						部门
	 * @param durationExpression
	 * @param localResource
	 * @param ywRqSsgdSqbForm
	 * @param sprzwdm
	 * @param sprzwmc
	 * @param iswb						是否网办
	 */
	public void commonZwBeginByJbrsl2(int flag, Object obj, String jc,String thisNoteActivityName,String nextNoteActivityName,String masterEntityKey,
			WfBusInstanceTaskForm instance,String loginUserName,String dpm, String durationExpression,
			ResourceBundle localResource, String sprzwdm,String sprzwmc,boolean iswb,Map<String,String> map){
		
		String link001 = localResource.getString(jc + "001");//受理
//		String link002 = localResource.getString(jc + "002");//承办
		String link003 = localResource.getString(jc + "003");//审核
		String link004 = localResource.getString(jc + "004");//审批
		String link005 = localResource.getString(jc + "005");//办结
		
		List<String> linkList =new ArrayList<String>();
		YwSphjInfo ywSphjInfo = this.getYwSphjInfo(masterEntityKey,instance.getTemplateCode());
		ywSphjInfo.setTemplateCode(instance.getTemplateCode());
		/*if(nextNoteActivityName.equals(link001)){//发给经办人初审，则受理、 承办
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				linkList.add("002");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
				ywSphjInfo.setIsChengban("yes");
			}
		}else */
		if(thisNoteActivityName.equals(link001)){//经办人初审环节
			if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
				linkList.add("001");
				linkList.add("002");
				ywSphjInfo.setMasterEntityKey(masterEntityKey);
				ywSphjInfo.setIsShouli("yes");
				ywSphjInfo.setIsChengban("yes");
			}
			if(nextNoteActivityName.equals("dzgz") || nextNoteActivityName.equals("ckfsgz")){//经办人初审环节 发到 电子盖章
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun() == null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie() == null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
			if(nextNoteActivityName.contains("end")){//经办人初审环节 直接结案
				if(ywSphjInfo.getIsShouli()==null || !ywSphjInfo.getIsShouli().equals("yes")){					
					linkList.add("001");
					ywSphjInfo.setMasterEntityKey(masterEntityKey);
					ywSphjInfo.setIsShouli("yes");
				}
				if(ywSphjInfo.getIsChengban()==null || !ywSphjInfo.getIsChengban().equals("yes")){					
					linkList.add("002");
					ywSphjInfo.setIsChengban("yes");
				}
				if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
					linkList.add("003");
					ywSphjInfo.setIsShenhe("yes");
				}
				if(ywSphjInfo.getIsPizhun() == null || !ywSphjInfo.getIsPizhun().equals("yes")){
					linkList.add("004");
					ywSphjInfo.setIsPizhun("yes");
				}
				if(ywSphjInfo.getIsBanjie() == null || !ywSphjInfo.getIsBanjie().equals("yes")){
					linkList.add("005");
					linkList.add("006");
					ywSphjInfo.setIsBanjie("yes");
				}
			}
		}else if(thisNoteActivityName.equals(link003)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
		}else if(thisNoteActivityName.equals(link004)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
		}else if(thisNoteActivityName.equals(link005)){
			if(ywSphjInfo.getIsShenhe() == null || !ywSphjInfo.getIsShenhe().equals("yes")){
				linkList.add("003");
				ywSphjInfo.setIsShenhe("yes");
			}
			if(ywSphjInfo.getIsPizhun()==null || !ywSphjInfo.getIsPizhun().equals("yes")){
				linkList.add("004");
				ywSphjInfo.setIsPizhun("yes");
			}
			if(ywSphjInfo.getIsBanjie()==null || !ywSphjInfo.getIsBanjie().equals("yes")){
				linkList.add("005");
				linkList.add("006");
				ywSphjInfo.setIsBanjie("yes");
			}
		}
		if(StringUtils.isNotBlank(ywSphjInfo.getMasterEntityKey())){
			this.saveOrUpdateYwSphjInfo(ywSphjInfo);
		}		
		this.saveZw(flag, thisNoteActivityName, obj, instance, loginUserName, iswb, durationExpression, sprzwdm, sprzwmc, linkList,map);
	}
		
	/**
	 * 获取YwSphjInfo实体
	 * @param masterEntityKey  业务id
	 * @param templateCode     业务code
	 * @return
	 */
	public YwSphjInfo getYwSphjInfo(String masterEntityKey,String templateCode){
		Page<YwSphjInfo> ywSphjInfosPage = new Page<YwSphjInfo>(10);
		ywSphjInfosPage = this.getByMasterEntityKey(ywSphjInfosPage , masterEntityKey,templateCode);
		List<YwSphjInfo> sphjList = ywSphjInfosPage.getResult();
		YwSphjInfo ywSphjInfo ;
		if(sphjList.size()>0){
			ywSphjInfo = sphjList.get(0);
		}else{
			ywSphjInfo = new YwSphjInfo();
		}
		return ywSphjInfo;
	}
	
	/**
	 * 调用 保存数据到YW_ZHENGWU_INFO
	 * @param flag
	 * @param thisNoteActivityName
	 * @param shljYsForm
	 * @param instance
	 * @param loginUserName
	 * @param iswb
	 * @param durationExpression
	 * @param serialNo
	 * @param password
	 * @param sprzwdm
	 * @param sprzwmc
	 * @param link
	 * @param isList
	 * @param linkList
	 */
	public void saveZw(int flag,String thisNoteActivityName,Object obj,WfBusInstanceTaskForm instance,String loginUserName,boolean iswb,
			String durationExpression,String sprzwdm,String sprzwmc,List<String> linkList,Map<String,String> map){
			try{
				if(linkList != null && linkList.size()>0){
					for(Iterator<String> i = linkList.iterator(); i.hasNext();){
						String link = i.next();
						WfUtilsEX.SendCompositeMessage(flag,thisNoteActivityName,obj,instance.getHandleComments(),instance.getHandleCommentsTime(),loginUserName,iswb,instance,durationExpression,link,ywZhengwuInfoService,ywComSjcllistService,ywComSjclService,sprzwdm,sprzwmc,
								ywPrintDocService, ywComQyxxService,oaDocCodeServiceEX,wfTemplateAttachmentRefServiceEX,map);
					}
				}
			}catch(Exception e){
				logger.error("(接口)保存政务中心交换信息出错"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
				System.out.println("保存政务中心交换信息出错");
			}
	}
	/**
	 * 生成经营许可和企业资质企业、站点信息XML，用于交换给金证
	 * @param instance
	 * @param masterEntityKey
	 * @param map
	 */
	public void createXML2Jz(WfBusInstanceTaskForm instance,String masterEntityKey,Map<String,String> map){
		try {
			List<YwPrintDocForm> ywPrintDocFormList = ywPrintDocService.getFile(instance.getTemplateCode(), Long.parseLong(masterEntityKey), "zyxzxkjds");
			if(ywPrintDocFormList != null && ywPrintDocFormList.size()>0){//办结时，如果通过，制作了准予许可决定书
				if(instance.getTemplateCode().contains("rqjyxkz")){//生成经营许可企业、站点、抢险点信息xml，用于交换给金证
					kingdomDataForRqsjService.getRqjyxkDatas("", "", (String)map.get("sqdw"),masterEntityKey,(String)map.get("qyid"));//经营许可企业信息
					kingdomDataForRqsjService.getGyzDatas("", "", (String)map.get("sqdw"),masterEntityKey,(String)map.get("qyid"));//站点-供应站信息
					kingdomDataForRqsjService.getJszDatas("", "", (String)map.get("sqdw"),masterEntityKey,(String)map.get("qyid"));//站点-接收站等信息
					kingdomDataForRqsjService.getQxdDatas("", "", (String)map.get("sqdw"),(String)map.get("acceptNo"),(String)map.get("qyid"));//抢险点信息
				}else if("rqrsqjazwxqyzz".equals(instance.getTemplateCode())){//生成企业资质企业信息xml，用于交换给金证
					kingdomDataForRqsjService.getRqQyzzDatas("", "", (String)map.get("sqdw"),masterEntityKey,(String)map.get("qyid"));//企业资质企业信息
				}
			}
		} catch (Exception e) {
			logger.error(instance.getTemplateCode() + "_" + masterEntityKey + "_" + (String)map.get("qyid")+"导入申请表时生成企业和站点xml出错"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
			e.printStackTrace();
		}
	}

	
}
